package io.renren.modules.lanling.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.lanling.entity.NoticeEntity;
import io.renren.modules.lanling.service.NoticeService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * 须知
 *
 * @author LANLING
 * @email lanling@gmail.com
 * @date 2018-12-03 14:21:12
 */
@RestController
@RequestMapping("lanling/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("lanling:notice:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = noticeService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("lanling:notice:info")
    public R info(@PathVariable("id") Integer id) {
        NoticeEntity notice = noticeService.selectById(id);

        return R.ok().put("notice", notice);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("lanling:notice:save")
    public R save(@RequestBody NoticeEntity notice) {
        notice.setCreateDate(new Date());
        noticeService.insert(notice);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("lanling:notice:update")
    public R update(@RequestBody NoticeEntity notice) {
        ValidatorUtils.validateEntity(notice);
        notice.setLastModifyDate(new Date());
        noticeService.updateAllColumnById(notice);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("lanling:notice:delete")
    public R delete(@RequestBody Integer[] ids) {
        noticeService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }

}
